package cloud.maque.blog.controller;


import cloud.maque.blog.entity.MqCategory;
import cloud.maque.blog.service.MqCategoryService;
import cloud.maque.common.core.request.PageRequest;
import cloud.maque.common.core.response.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类表;(MQ_CATEGORY)表控制层
 *
 * @author : maque.cloud
 * @date : 2025-2-5
 */
@Tag(name = "分类表对象功能接口")
@RestController
@RequestMapping("/category")
public class MqCategoryController {
    @Autowired
    private MqCategoryService mqCategoryService;

    /**
     * 通过ID查询单条数据
     *
     * @param categoryId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/detail/{categoryId}")
    public R<MqCategory> queryById(@PathVariable(value = "categoryId") String categoryId) {

        MqCategory data = mqCategoryService.queryById(categoryId);
        return R.ok("ok", data);
    }

    /**
     * 分页查询
     *
     * @param pageReq 筛选条件
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/list")
    public R<List<MqCategory>> paginQuery(@RequestBody PageRequest<MqCategory> pageReq) {
        //1.分页参数
        long current = pageReq.getPageIndex();
        long size = pageReq.getPageSize();
        //2.分页查询
        IPage<MqCategory> pageResult = mqCategoryService.paginQuery(pageReq.getData(), current, size);
        return R.ok("OK", pageResult);
    }

    /**
     * 新增数据
     *
     * @param mqCategory 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/save")
    public ResponseEntity<MqCategory> add(@RequestBody MqCategory mqCategory) {
        return ResponseEntity.ok(mqCategoryService.Save(mqCategory));
    }

    /**
     * 通过主键删除数据
     *
     * @param categoryId 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据")
    @DeleteMapping("/delete/{categoryId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("categoryId") String categoryId) {
        return ResponseEntity.ok(mqCategoryService.deleteById(categoryId));
    }
}